Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Cómo convertir un objeto de datos { "key.subkey": "value" } a { "key" : "subkey": { "value" } }

Quiero convertir un objeto de datos de:

 let data = { "data.name" : "michael", "data.email" : "michael@xsx.com", "person.gender" : "male" }

dentro

 { "data" : { "name": "michael", "email": "michael@xsx.com" }, "person" : { "gender": "male" } }

Probé un bucle e intenté dividir la clave:

 let newdata = new Object() for (const property in datas) { let key = `${property.split(".")[0]}` let subkey = `${property.split(".")[1]}` newdata['key'] = key; newdata['value'] = {subkey: `${datas[property]}`}; console.log(newdata); }

El resultado se ve así:

 "data" : { "name" : { "michael" } }, "data" : { "email" : { "michael@xsxx.com" } }, "person" : { "gender" : { "male" } }

¿Que sigue?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar Object.entries para obtener los pares clave/valor de sus datos existentes, luego use reduce para convertir eso en un nuevo objeto con las claves divididas formando un objeto anidado:

 const data = { "data.name" : "michael", "data.email" : "michael@xsx.com", "person.gender" : "male" } const result = Object.entries(data).reduce((acc, [key, value]) => { [key, subkey] = key.split('.') acc[key] = acc[key] || {} acc[key][subkey] = value return acc }, {}) console.log(result)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!